package com.buding.fx.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.buding.fx.common.constant.CssStyleInfo;
import com.buding.fx.common.enums.ButtonTypeEnum;
import com.buding.fx.dao.TagDao;
import com.buding.fx.dao.TagGroupDao;
import com.buding.fx.dao.impl.TagDaoImpl;
import com.buding.fx.dao.impl.TagGroupDaoImpl;
import com.buding.fx.entity.Menu;
import com.buding.fx.entity.SystemConfigInfo;
import com.buding.fx.entity.Tag;
import com.buding.fx.entity.TagGroup;
import com.buding.fx.service.CenterPageBuildService;
import com.buding.fx.utils.ButtonUtils;
import com.sun.javafx.sg.prism.NGRectangle;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author mingdongwei-jwk
 * @date 2022/10/10 16:45
 * @class com.buding.fx.service.impl
 * @Description
 */
public class TagManagerBuildService extends BasePageBuildSerivce implements CenterPageBuildService {

    private double wight = 150;   //高度

    private String font_family = "Franklin Gothic Demi Cond";

    @Override
    public Node buildTopNode(Menu menu, SystemConfigInfo configInfo) {
        return addTitle(configInfo.getCss(),menu.getPath());
    }

    @Override
    public Node buildSearchNode(Menu menu, SystemConfigInfo configInfo) {
        CssStyleInfo css = configInfo.getCss();
        VBox vbox = new VBox();
        vbox.setPrefWidth(css.getCenter_weight()-css.getCenter_content_left());  //注意外面会将中间部分左偏移
        vbox.setMaxHeight(css.getCenter_height()-css.getCenter_title_height()-100);
        Border border = new Border(new BorderStroke(Color.web("#A2BEE2"), BorderStrokeStyle.SOLID,null,new BorderWidths(1,0,1,1)));
        vbox.setBorder(border);
        String style = "-fx-background-color: #F2F2F2";
        vbox.setStyle(style);
        vbox.setAlignment(Pos.CENTER);
       // flowPane.setOrientation(Orientation.HORIZONTAL);

        GridPane gridPane = new GridPane();
        gridPane.setPrefHeight(35);
        gridPane.setPrefWidth(vbox.getPrefWidth());
        gridPane.setStyle("-fx-background-color: #eaeff6");
        Border border2 = new Border(new BorderStroke(Color.web("#A2BEE2"), BorderStrokeStyle.SOLID,null,new BorderWidths(0,0,1,0)));
        gridPane.setBorder(border2);

        double width  = gridPane.getPrefWidth()/2;
        HBox hbox1 = new HBox();
        hbox1.setPrefWidth(width);
        Label label = new Label("标签分类列表");
        Font font  = Font.font(font_family,null,null,13);
        label.setFont(font);
        label.setTextFill(Color.web("#0A0A0A"));
        hbox1.setAlignment(Pos.CENTER_LEFT);
        hbox1.getChildren().add(label);

        HBox hbox2 = new HBox();
        hbox2.setPrefWidth(width);

        //Button button2 = ButtonUtils.createButton(70,10,ButtonTypeEnum.ADD,"添加分类");
        Button button2 = new Button();
        button2.setText("#8c8c8c");
        button2.getStyleClass().setAll("btn-sm","btn-success");

        hbox2.setAlignment(Pos.CENTER_RIGHT);
        hbox2.getChildren().add(button2);

        HBox.setMargin(label,new Insets(0,0,0,5));
        HBox.setMargin(button2,new Insets(0,20,0,0));
        gridPane.add(hbox1,0,0);
        gridPane.add(hbox2,1,0);
        gridPane.setAlignment(Pos.CENTER);
        vbox.getChildren().add(gridPane);

        VBox contentVbox = new VBox();
        List<VBox> tagNodes = getTagNodes(vbox);
        contentVbox.getChildren().addAll(tagNodes);
        contentVbox.setPrefWidth(vbox.getPrefWidth());
        contentVbox.setMaxHeight(vbox.getMaxHeight()-gridPane.getMinHeight());
        ScrollPane scrollBar = new ScrollPane();
        scrollBar.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        scrollBar.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
        scrollBar.setContent(contentVbox);
        scrollBar.setMaxHeight(contentVbox.getMaxHeight());
        scrollBar.setStyle("-fx-background-color: white");
        vbox.getChildren().addAll(scrollBar,contentVbox);
        return vbox;
    }

    @Override
    public Node bulidSerchResultNode(Menu menu, SystemConfigInfo configInfo) {
        return null;
    }


    private List<VBox> getTagNodes(VBox parantVbox){
        List<VBox> r = new ArrayList<>();
        TagDao tagDao = new TagDaoImpl();
        TagGroupDao tagGroupDao = new TagGroupDaoImpl();
        List<TagGroup> tagGroups = tagGroupDao.selectAll();
        List<Tag> tags = tagDao.selectAll();
        Map<Long, List<Tag>> collect = tags.stream().filter(itm->itm.getTagGroupId()!=null).collect(Collectors.groupingBy(Tag::getTagGroupId));
        if (CollectionUtil.isNotEmpty(tagGroups)){
            for (TagGroup tagGroup:tagGroups){
                String tagGroupName = tagGroup.getName();
                VBox gropVbox = new VBox();
                String stlye =  "-fx-background-color: #ffffff";
                gropVbox.setStyle(stlye);
                gropVbox.setPrefWidth(parantVbox.getPrefWidth()-50);
                gropVbox.setMaxWidth(parantVbox.getPrefWidth()-50);
               // gropVbox.setMaxHeight(100);
                gropVbox.setStyle(stlye);
                gropVbox.setAlignment(Pos.CENTER_LEFT);
                GridPane gridPane = new GridPane();
                gridPane.setPrefWidth(gropVbox.getPrefWidth());
                gridPane.setAlignment(Pos.CENTER);
                double width = gropVbox.getPrefWidth()/2;
                HBox b1 = new HBox();
                b1.setPrefWidth(width);
                b1.setAlignment(Pos.CENTER_LEFT);

                Label label = new Label(tagGroupName);
                label.setTextFill(Color.web("#787975"));
                label.setFont(Font.font(14 ));

                HBox.setMargin(label,new Insets(0,0,0,10));
                b1.getChildren().add(label);


                HBox b2 = new HBox();
                b2.setPrefWidth(width);
                b2.setAlignment(Pos.CENTER_RIGHT);
                //b2.setStyle("-fx-background-color: #b5d3e3");
                b2.setSpacing(10);
                Button editButton = new Button();
                editButton.setText("编辑标签组");
                editButton.getStyleClass().setAll("btn-xs","btn-success");
                editButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        Alert alert = new Alert(Alert.AlertType.INFORMATION);
                        alert.show();
                    }
                });


                Button deleteButton = new Button();
                deleteButton.setText("删除标签组");
                deleteButton.getStyleClass().setAll("btn-xs","btn-danger");

                Button deleteButton1 = new Button();
                deleteButton1.setText("删除标签");
                deleteButton1.getStyleClass().setAll("btn-xs","btn-warning");
                deleteButton1.getProperties().put("tagGroupId",tagGroup.getId());





                b2.getChildren().addAll(editButton,deleteButton,deleteButton1);

                HBox.setMargin(b2,new Insets(0,10,0,0));
                gridPane.add(b1,0,0);
                gridPane.add(b2,1,0);
                gropVbox.getChildren().add(gridPane);
                VBox.setMargin(gropVbox,new Insets(10,10,10,10));
                Separator separator = new Separator();
                separator.setOrientation(Orientation.HORIZONTAL);
                gropVbox.getChildren().add(separator);


                FlowPane hBox = new FlowPane();
                hBox.setPrefWidth(gropVbox.getPrefWidth());
                hBox.setMaxWidth(gropVbox.getPrefWidth());
                hBox.setHgap(15);
                hBox.setVgap(10);
                hBox.setPadding(new Insets(0,0,0,10));
                hBox.setPrefHeight(35);
                hBox.setAlignment(Pos.CENTER_LEFT);
                List<Tag> thistags = collect.get(tagGroup.getId());
                if (CollectionUtil.isNotEmpty(thistags)){
                    for (Tag tag: thistags){
                        Label l = new Label(tag.getName());
                       // l.setStyle("-fx-background-color: #F4F4F4; -fx-font-size: 13; -fx-text-fill: #868485;");
                        l.getStyleClass().setAll("lbl-sm","lbl","lbl-info");
                        hBox.getChildren().add(l);
                    }
                }
                hBox.getChildren().add(createAddButton("添加标签"));

                Button button = new Button( "删除");
                button.getStyleClass().setAll("btn-xs","btn-info");
                Image image = new Image("img/icon/lable-delete.png");
                ImageView imageView = new ImageView(image);
                imageView.setFitWidth(15);
                imageView.setFitHeight(15);
                button.setGraphic(imageView);
                button.setContentDisplay(ContentDisplay.RIGHT);
               // hBox.getChildren().add(button);



                gropVbox.getChildren().add(hBox);
                VBox.setMargin(separator,new Insets(0,0,0,10));
                VBox.setMargin(gridPane,new Insets(5,0,0,0));
                r.add(gropVbox);
            }


        }

        return r;
    }

    private Button createAddButton(String buttonName){
        Button label = new Button(buttonName);
        label.setStyle("-fx-background-color: white; -fx-text-fill: #878484");
        label.setFont(Font.font(12));
        Border border = new Border(new BorderStroke(Color.web("#8F8F8F"), BorderStrokeStyle.DASHED,new CornerRadii(5),new BorderWidths(1,1,1,1)));
        label.setBorder(border);
        Image image = new Image("img/icon/add-gray.png");
        ImageView imageView = new ImageView(image);
        imageView.setFitWidth(15);
        imageView.setFitHeight(15);
        label.setGraphic(imageView);
        label.setGraphicTextGap(1);
        return label;
    }
}
